import { createWebHistory, createRouter } from "vue-router";

// 定义路由配置数组
const routes = [
  {
    path: "/",
    // 重定向到登录页
    redirect: "/login",
  },
  {
    path: "/login",
    // 动态导入登录页面组件
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/location-weather",
    // 动态导入位置天气页面组件
    component: () => import("../views/LocationWeather.vue"),
  },
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});

// 添加导航守卫进行权限验证
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn'); // 假设登录状态存储在 localStorage 中
  if (to.path === '/login') {
    next();
  } else if (!isLoggedIn) {
    next('/login');
  } else {
    next();
  }
});

// 导出路由实例
export default router;